package com.yy.finvideoplayer.model

import android.os.Parcel
import android.os.Parcelable

/**
 * CoverView、CoverImage参数
 */
data class CoverParams(
    var accessibility: Boolean?,
    var ariaLabel: String?,
    var ariaRole: String?,
    var clickable: Boolean?,
    var `data`: String?,
    var fixed: Boolean?,
    var gesture: Boolean?,
    var inKeyboardAccessory: Boolean?,
    var label: Label?,
    var notCoverRect: Boolean?,
    val parentId: String?,
    var position: Position?,
    var sendTo: String?,
    var style: Style?,
    var url: String?,
    val viewId: String,
    var zIndex: Int?,
    val type: String?,
    var inScrollCoverView: Boolean?
) : Parcelable {

    constructor(parcel: Parcel) : this(
        parcel.readValue(Boolean::class.java.classLoader) as? Boolean,
        parcel.readString(),
        parcel.readString(),
        parcel.readValue(Boolean::class.java.classLoader) as? Boolean,
        parcel.readString(),
        parcel.readValue(Boolean::class.java.classLoader) as? Boolean,
        parcel.readValue(Boolean::class.java.classLoader) as? Boolean,
        parcel.readValue(Boolean::class.java.classLoader) as? Boolean,
        parcel.readParcelable(Label::class.java.classLoader),
        parcel.readValue(Boolean::class.java.classLoader) as? Boolean,
        parcel.readString(),
        parcel.readParcelable(Position::class.java.classLoader),
        parcel.readString(),
        parcel.readParcelable(Style::class.java.classLoader),
        parcel.readString(),
        parcel.readString()!!,
        parcel.readValue(Int::class.java.classLoader) as? Int,
        parcel.readString(),
        parcel.readValue(Boolean::class.java.classLoader) as? Boolean)

    fun update(coverParams: CoverParams) {
        coverParams.accessibility?.run { accessibility = this }
        coverParams.ariaLabel?.run { ariaLabel = this }
        coverParams.ariaRole?.run { ariaRole = this }
        coverParams.clickable?.run { clickable = this }
        coverParams.data?.run { data = this }
        coverParams.fixed?.run { fixed = this }
        coverParams.gesture?.run { gesture = this }
        coverParams.inKeyboardAccessory?.run { inKeyboardAccessory = this }
        coverParams.label?.run {
            when (label) {
                null -> label = this
                else -> label?.update(this)
            }
        }
        coverParams.notCoverRect?.run { notCoverRect = this }
        coverParams.position?.run {
            when (position) {
                null -> position = this
                else -> position?.update(this)
            }
        }
        coverParams.sendTo?.run { sendTo = this }
        coverParams.style?.run {
            when (style) {
                null -> style = this
                else -> style?.update(this)
            }
        }
        coverParams.url?.run { url = this }
        coverParams.zIndex?.run { zIndex = this }
        coverParams.inScrollCoverView?.run { inScrollCoverView = this }
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeValue(accessibility)
        parcel.writeString(ariaLabel)
        parcel.writeString(ariaRole)
        parcel.writeValue(clickable)
        parcel.writeString(data)
        parcel.writeValue(fixed)
        parcel.writeValue(gesture)
        parcel.writeValue(inKeyboardAccessory)
        parcel.writeParcelable(label, flags)
        parcel.writeValue(notCoverRect)
        parcel.writeString(parentId)
        parcel.writeParcelable(position, flags)
        parcel.writeString(sendTo)
        parcel.writeParcelable(style, flags)
        parcel.writeString(url)
        parcel.writeString(viewId)
        parcel.writeValue(zIndex)
        parcel.writeString(type)
        parcel.writeValue(inScrollCoverView)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<CoverParams> {
        override fun createFromParcel(parcel: Parcel): CoverParams {
            return CoverParams(parcel)
        }

        override fun newArray(size: Int): Array<CoverParams?> {
            return arrayOfNulls(size)
        }
    }
}

/**
 * CoverView文本内容参数
 */
data class Label(
    var color: String?,
    var content: String?,
    var fontSize: Float?,
    var letterSpacing: Float?,
    var fontWeight: String?,
    var lineBreak: String?,
    var lineHeight: Double?,
    var textAlign: String?,
    var whiteSpace: String?
) : Parcelable {

    constructor(parcel: Parcel) : this(
        parcel.readString(),
        parcel.readString(),
        parcel.readValue(Float::class.java.classLoader) as? Float,
        parcel.readValue(Float::class.java.classLoader) as? Float,
        parcel.readString(),
        parcel.readString(),
        parcel.readValue(Double::class.java.classLoader) as? Double,
        parcel.readString(),
        parcel.readString())

    fun update(label: Label) {
        label.color?.run { color = this }
        label.content?.run { content = this }
        label.fontSize?.run { fontSize = this }
        label.letterSpacing?.run { letterSpacing = this }
        label.fontWeight?.run { fontWeight = this }
        label.lineBreak?.run { lineBreak = this }
        label.lineHeight?.run { lineHeight = this }
        label.textAlign?.run { textAlign = this }
        label.whiteSpace?.run { whiteSpace = this }
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(color)
        parcel.writeString(content)
        parcel.writeValue(fontSize)
        parcel.writeValue(letterSpacing)
        parcel.writeString(fontWeight)
        parcel.writeString(lineBreak)
        parcel.writeValue(lineHeight)
        parcel.writeString(textAlign)
        parcel.writeString(whiteSpace)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<Label> {
        override fun createFromParcel(parcel: Parcel): Label {
            return Label(parcel)
        }

        override fun newArray(size: Int): Array<Label?> {
            return arrayOfNulls(size)
        }
    }
}

/**
 * CoverView位置参数
 */
data class Position(
    var height: Float?,
    var left: Float?,
    var top: Float?,
    var width: Float?,
    var offsetLeft: Float?,
    var offsetTop: Float?
) : Parcelable {

    constructor(parcel: Parcel) : this(
        parcel.readValue(Float::class.java.classLoader) as? Float,
        parcel.readValue(Float::class.java.classLoader) as? Float,
        parcel.readValue(Float::class.java.classLoader) as? Float,
        parcel.readValue(Float::class.java.classLoader) as? Float,
        parcel.readValue(Float::class.java.classLoader) as? Float,
        parcel.readValue(Float::class.java.classLoader) as? Float
    )

    fun update(position: Position) {
        position.height?.run { height = this }
        position.left?.run { left = this }
        position.top?.run { top = this }
        position.width?.run { width = this }
        position.offsetLeft?.run { offsetLeft = this }
        position.offsetTop?.run { offsetTop = this }
    }

    override fun toString(): String {
        return "Position(offsetLeft=$offsetLeft, offsetTop=$offsetTop, left=$left, top=$top, width=$width, height=$height)"
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeValue(height)
        parcel.writeValue(left)
        parcel.writeValue(top)
        parcel.writeValue(width)
        parcel.writeValue(offsetLeft)
        parcel.writeValue(offsetTop)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<Position> {
        override fun createFromParcel(parcel: Parcel): Position {
            return Position(parcel)
        }

        override fun newArray(size: Int): Array<Position?> {
            return arrayOfNulls(size)
        }
    }
}

/**
 * CoverView、CoverImage样式参数
 */
data class Style(
    var bgColor: String?,
    var borderColor: String?,
    var borderRadius: Float?,
    var borderWidth: Float?,
    var contentSize: ContentSize?,
    var fixed: Boolean?,
    var needScrollEvent: Boolean?,
    var opacity: Float?,
    var overflow: String?,
    var padding: List<Double>?,
    var rotate: Float?,
    var scaleX: Float?,
    var scaleY: Float?,
    var zIndex: Int?
) : Parcelable {

    constructor(parcel: Parcel) : this(
        parcel.readString(),
        parcel.readString(),
        parcel.readValue(Float::class.java.classLoader) as? Float,
        parcel.readValue(Float::class.java.classLoader) as? Float,
        parcel.readParcelable(ContentSize::class.java.classLoader),
        parcel.readValue(Boolean::class.java.classLoader) as? Boolean,
        parcel.readValue(Boolean::class.java.classLoader) as? Boolean,
        parcel.readValue(Float::class.java.classLoader) as? Float,
        parcel.readString(),
        parcel.createDoubleArray()?.toList(),
        parcel.readValue(Float::class.java.classLoader) as? Float,
        parcel.readValue(Float::class.java.classLoader) as? Float,
        parcel.readValue(Float::class.java.classLoader) as? Float,
        parcel.readValue(Int::class.java.classLoader) as? Int)

    fun update(style: Style) {
        style.bgColor?.run { bgColor = this }
        style.borderColor?.run { borderColor = this }
        style.borderRadius?.run { borderRadius = this }
        style.borderWidth?.run { borderWidth = this }
        style.contentSize?.run {
            when (contentSize) {
                null -> contentSize = this
                else -> contentSize?.update(this)
            }
        }
        style.fixed?.run { fixed = this }
        style.needScrollEvent?.run { needScrollEvent = this }
        style.opacity?.run { opacity = this }
        style.overflow?.run { overflow = this }
        style.padding?.run { padding = this }
        style.rotate?.run { rotate = this }
        style.scaleX?.run { scaleX = this }
        style.scaleY?.run { scaleY = this }
        style.zIndex?.run { zIndex = this }
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(bgColor)
        parcel.writeString(borderColor)
        parcel.writeValue(borderRadius)
        parcel.writeValue(borderWidth)
        parcel.writeParcelable(contentSize, flags)
        parcel.writeValue(fixed)
        parcel.writeValue(needScrollEvent)
        parcel.writeValue(opacity)
        parcel.writeString(overflow)
        parcel.writeDoubleArray(padding?.toDoubleArray())
        parcel.writeValue(rotate)
        parcel.writeValue(scaleX)
        parcel.writeValue(scaleY)
        parcel.writeValue(zIndex)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<Style> {
        override fun createFromParcel(parcel: Parcel): Style {
            return Style(parcel)
        }

        override fun newArray(size: Int): Array<Style?> {
            return arrayOfNulls(size)
        }
    }
}

data class ContentSize(
    var width: Float?,
    var height: Float?
) : Parcelable {

    constructor(parcel: Parcel) : this(
        parcel.readValue(Float::class.java.classLoader) as? Float,
        parcel.readValue(Float::class.java.classLoader) as? Float)

    fun update(contentSize: ContentSize) {
        contentSize.width?.run { width = this }
        contentSize.height?.run { height = this }
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeValue(width)
        parcel.writeValue(height)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<ContentSize> {
        override fun createFromParcel(parcel: Parcel): ContentSize {
            return ContentSize(parcel)
        }

        override fun newArray(size: Int): Array<ContentSize?> {
            return arrayOfNulls(size)
        }
    }
}

/**
 * 移除CoverView、CoverImage的参数
 */
data class RemoveParams(
    val viewId: String
) : Parcelable {
    constructor(parcel: Parcel) : this(parcel.readString()!!)

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeString(viewId)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<RemoveParams> {
        override fun createFromParcel(parcel: Parcel): RemoveParams {
            return RemoveParams(parcel)
        }

        override fun newArray(size: Int): Array<RemoveParams?> {
            return arrayOfNulls(size)
        }
    }
}

/**
 * CoverView、CoverImage动画参数
 */
data class AnimateParams(
    val duration: Long,
    val easing: String,
    val finalStyle: FinalStyle?,
    val viewId: String
) : Parcelable {
    constructor(parcel: Parcel) : this(
        parcel.readLong(),
        parcel.readString()!!,
        parcel.readParcelable(FinalStyle::class.java.classLoader),
        parcel.readString()!!)

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeLong(duration)
        parcel.writeString(easing)
        parcel.writeParcelable(finalStyle, flags)
        parcel.writeString(viewId)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<AnimateParams> {
        override fun createFromParcel(parcel: Parcel): AnimateParams {
            return AnimateParams(parcel)
        }

        override fun newArray(size: Int): Array<AnimateParams?> {
            return arrayOfNulls(size)
        }
    }
}

/**
 * CoverView、CoverImage经过动画变换后的最终样式
 */
data class FinalStyle(
    val left: Float?,
    val opacity: Float?,
    val rotate: Float?,
    val scaleX: Float?,
    val scaleY: Float?,
    val top: Float?,
    var offsetLeft: Float?,
    var offsetTop: Float?
) : Parcelable {
    constructor(parcel: Parcel) : this(
        parcel.readValue(Float::class.java.classLoader) as? Float,
        parcel.readValue(Float::class.java.classLoader) as? Float,
        parcel.readValue(Float::class.java.classLoader) as? Float,
        parcel.readValue(Float::class.java.classLoader) as? Float,
        parcel.readValue(Float::class.java.classLoader) as? Float,
        parcel.readValue(Float::class.java.classLoader) as? Float,
        parcel.readValue(Float::class.java.classLoader) as? Float,
        parcel.readValue(Float::class.java.classLoader) as? Float
    )

    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeValue(left)
        parcel.writeValue(opacity)
        parcel.writeValue(rotate)
        parcel.writeValue(scaleX)
        parcel.writeValue(scaleY)
        parcel.writeValue(top)
        parcel.writeValue(offsetLeft)
        parcel.writeValue(offsetTop)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<FinalStyle> {
        override fun createFromParcel(parcel: Parcel): FinalStyle {
            return FinalStyle(parcel)
        }

        override fun newArray(size: Int): Array<FinalStyle?> {
            return arrayOfNulls(size)
        }
    }
}